package com.deyuanyun.pic.settings.service.org.impl;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.org.DeptDTO;
import com.deyuanyun.pic.settings.controller.dto.org.DeptVO;
import com.deyuanyun.pic.settings.controller.dto.org.PostVO;
import com.deyuanyun.pic.settings.domain.org.Dept;
import com.deyuanyun.pic.settings.domain.org.OrgInfo;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.settings.mapper.org.DeptMapper;
import com.deyuanyun.pic.settings.mapper.org.OrgInfoMapper;
import com.deyuanyun.pic.settings.mapper.org.PostMapper;
import com.deyuanyun.pic.settings.service.org.DeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {
	
	@Resource
	DeptMapper deptMapper;
	@Resource
	private PostMapper postMapper;
	@Resource
	OrgInfoMapper orgInfoMapper;

    /**
     * 查询部门信息
     * @param deptVO
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
	@Override
	public List<DeptDTO> queryAll(DeptVO deptVO) {
		return deptMapper.queryAll(deptVO);
	}

    /**
     * 保存部门信息
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
    @Override
    public void saveInfo(DeptVO deptVO) {
        String shortName = deptVO.getShortName();
        Asserts.assertTrue(!StringUtils.isEmpty(shortName), "部门名称不能为空");
        Dept info = new Dept();
        BeanUtils.copyProperties(deptVO, info);
        info.setState((short) 0);
        DomainInitUtil.initCreateDomain(info);

        Asserts.assertTrue(!StringUtils.isEmpty(info.getCompanyId()), "上级公司不能为空");

        DeptVO vo = new DeptVO();
        vo.setShortName(shortName);
        List<DeptDTO> deptDTOS = deptMapper.queryAll(vo);
        Asserts.assertTrue(deptDTOS.isEmpty(), "系统已经存在相同的部门名称");
        deptMapper.insertSelective(info);

        String parentId = info.getParentId();
        OrgInfo record = new OrgInfo();

        BeanUtils.copyProperties(info, record);
        record.setType("1");
        record.setOrgId(info.getId());
        record.setParentId(StringUtils.isNotEmpty(parentId) ? parentId : info.getCompanyId());
        record.setIdPath(info.getId());
        if(StringUtils.isNotEmpty(parentId)){
            Map map = orgInfoMapper.findOrg(parentId);
            record.setIdPath(map.get("ID_PATH") + "#" + record.getIdPath());
        }
        orgInfoMapper.insertSelective(record);
    }

    /**
     * 更新部门信息
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
    @Override
    public void updateInfo(DeptVO deptVO) {
	    Dept info = new Dept();
        BeanUtils.copyProperties(deptVO, info);
        DomainInitUtil.initUpdateDomain(info);
        String id = info.getId();
        String parentId = info.getParentId();
        Asserts.assertTrue(!id.equals(parentId), "上级部门不能是自己");

        String shortName = info.getShortName();
        Asserts.assertTrue(!StringUtils.isEmpty(shortName), "部门名称不能为空");

        Asserts.assertTrue(!StringUtils.isEmpty(info.getCompanyId()), "上级公司不能为空");

        DeptVO vo = new DeptVO();
        vo.setId(deptVO.getId());
        vo.setShortName(shortName);
        List page = deptMapper.queryAll(vo);
        Asserts.assertTrue(page.isEmpty(), "系统已经存在相同的部门名称");
        if(StringUtils.isNotEmpty(parentId)) {
            Asserts.assertTrue(getParent(id, parentId, deptMapper.queryAll(new DeptVO())), "上级部门错误，不能为部门的下级公司");
        }
        deptMapper.updateByPrimaryKeySelective(info);
        OrgInfo record = new OrgInfo();
        BeanUtils.copyProperties(info, record);
        record.setType("1");
        record.setOrgId(id);
        record.setParentId(StringUtils.isNotEmpty(parentId) ? parentId : info.getCompanyId());
        record.setIdPath(id);
        if(StringUtils.isNotEmpty(parentId)){
            Map map = orgInfoMapper.findOrg(parentId);
            record.setIdPath(map.get("ID_PATH") + "#" + record.getIdPath());
        }
        orgInfoMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 根据id删除部门信息
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
	@Override
	public int deleteInfo(List<String> idList) {
		HashMap search = new HashMap();
        search.put("idList", idList);
        PostVO postVO = new PostVO();
        postVO.setDeptIdList(idList);
		List page = postMapper.queryAll(postVO);
		Asserts.assertTrue(page.isEmpty(), "部门不能被删除,下面还存在岗位");

		DeptVO deptVO = new DeptVO();
		deptVO.setParentIdList(idList);
		page = deptMapper.queryAll(deptVO);
        Asserts.assertTrue(page.isEmpty(), "部门不能被删除,还有下级部门");

		int n = deptMapper.deleteByPrimaryKey(search);
        for(int i = 0, len = idList.size(); i < len; i++) {
            String id = idList.get(i);
            if(StringUtils.isNotEmpty(id)) {
                orgInfoMapper.deleteByPrimaryKey(id);
            }
        }
		return n;
	}

	/**
	 * 更新部门启用状态
	 * @param
	 * @return 
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-12-30
	 */
	@Override
	public int updateState(HashMap param) {
		return deptMapper.updateState(param);
	}

	private boolean getParent(String path,String id,List<DeptDTO> data){
		if(id == null)
		{
			return true;
		}
		for(DeptDTO c : data)
		{
			String cid = c.getId();
			String cpid = c.getParentId();
			if(id.equals(cid)){
				if(!StringUtils.isEmpty(cpid)) {
					if(path.indexOf(cpid) >= 0) {
						return false;
					}
					return getParent(cpid+"->"+path,cpid,data);
				}else{
					return true;
				}
			}
		}
		return false;
	}
}
